// pages/fahuoqingdan/fahuoqingdan.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type: '',
    shippingList: [],
    selectregionainfo: '',
    nowpage: 1,
    sumpage: 1,
    dingdanTimes2: '',
    flag: '',
    shippingSongList: [],
    showflag: false
  },
  // 获取数据
  getshoppingList() {
    let that = this
    let timesobj = that.data.dingdanTimes2
    let star, end
    if (that.data.huoindex == 1) {
      star = ''
      end = ''
    } else {
      star = timesobj.yearstar + '-' + timesobj.monthstar + '-' + timesobj.daystar + ' 00:00:00'
      end = timesobj.yearend + '-' + timesobj.monthend + '-' + timesobj.dayend + ' 23:59:59'
    }
    let recetownId
    if (that.data.selectregionainfo.townId) {
      recetownId = that.data.selectregionainfo.townId
    } else {
      recetownId = ''
    }
    wx.request({
      url: app.globalData.myurl,
      data: {
        cmd: "shippingList",
        userId: wx.getStorageSync('userId'),
        shopId: wx.getStorageSync('shopId'),
        type: that.data.type,
        beginAdtime: star,
        endAdtime: end,
        nowPage: that.data.nowpage,
        recetownId: recetownId
      },
      header: {
        "content-type": "application/x-www-form-urlencoded",
        "token": wx.getStorageSync('token')
      },
      success(res) {
        if (res.data.zjstate == 1) {
          setTimeout(function () {
            wx.hideLoading()
            wx.stopPullDownRefresh() //停止下拉刷新
          }, 0)
          setTimeout(function () {
            wx.showModal({
              title: '您的账号已在其他设备登录，请重新登录！',
              showCancel: false, //是否显示取消按钮false去掉取消按钮
              success: function (res) {
                if (res.confirm) {
                  wx.clearStorageSync()
                  wx.reLaunch({
                    url: '/pages/login/login',
                  })
                } else {
                  wx.clearStorageSync()
                  wx.reLaunch({
                    url: '/pages/login/login',
                  })
                }
              }
            })
          }, 0)
          return
        }
        console.log(res)
        if (res.data.result == 1) {
          setTimeout(function() {
            wx.hideLoading()
          }, 0)
          setTimeout(function() {
            wx.showToast({
              title: res.data.resultNote,
              icon: 'none',
              duration: 1500
            })
          }, 0)
        } else {
          let goodssuamlist = []
          let goodsindexlist = []
          let obj = res.data.goodsBean
          let area = obj.goodsList
          for (let j in area) {
            let goodsobj = {
              goodsId: area[j].goodsId,
              goodsname: area[j].goodsname,
              guigelist: []
            }
            goodsobj.guigelist.push({
              val: area[j].value1,
              num: area[j].num,
              skuid: area[j].goodSkuId
            })
            if (goodsindexlist.indexOf(area[j].goodsId) > -1) {
              let indexs = goodsindexlist.indexOf(area[j].goodsId)
              goodssuamlist[indexs].guigelist.push({
                val: area[j].value1,
                num: area[j].num,
                skuid: area[j].goodSkuId
              })
            } else {
              goodssuamlist.push(goodsobj)
              goodsindexlist.push(area[j].goodsId)
            }
            console.log(goodsobj)
          }
          console.log(goodssuamlist)
          obj.goodsList = goodssuamlist

          that.setData({
            shippingList: obj,
            showflag: true
          })
          setTimeout(function() {
            wx.hideLoading()
          }, 0)
        }
      }
    });
  },
  // 获取送的规格数据
  getSongshoppingList() {
    let that = this
    let timesobj = that.data.dingdanTimes2
    let star, end
    if (that.data.huoindex == 1) {
      star = ''
      end = ''
    } else {
      star = timesobj.yearstar + '-' + timesobj.monthstar + '-' + timesobj.daystar + ' 00:00:00'
      end = timesobj.yearend + '-' + timesobj.monthend + '-' + timesobj.dayend + ' 23:59:59'
    }
    let recetownId
    if (that.data.selectregionainfo.townId) {
      recetownId = that.data.selectregionainfo.townId
    } else {
      recetownId = ''
    }
    wx.request({
      url: app.globalData.myurl,
      data: {
        cmd: "giveList",
        userId: wx.getStorageSync('userId'),
        shopId: wx.getStorageSync('shopId'),
        type: that.data.type,
        beginAdtime: star,
        endAdtime: end,
        recetownId: recetownId
      },
      header: {
        "content-type": "application/x-www-form-urlencoded",
        "token": wx.getStorageSync('token')
      },
      success(res) {
        console.log(res, '送')
        if (res.data.result == 1) {
          setTimeout(function() {
            wx.hideLoading()
          }, 0)
          setTimeout(function() {
            wx.showToast({
              title: res.data.resultNote,
              icon: 'none',
              duration: 1500
            })
          }, 0)
        } else {
          let goodssuamlist = []
          let goodsindexlist = []
          let obj = res.data.goodsBean
          let area = obj.goodsList
          for (let j in area) {
            let goodsobj = {
              goodsId: area[j].goodsId,
              goodsname: area[j].goodsname,
              guigelist: []
            }
            goodsobj.guigelist.push({
              val: area[j].value1,
              num: area[j].num,
              skuid: area[j].goodSkuId
            })
            if (goodsindexlist.indexOf(area[j].goodsId) > -1) {
              let indexs = goodsindexlist.indexOf(area[j].goodsId)
              goodssuamlist[indexs].guigelist.push({
                val: area[j].value1,
                num: area[j].num,
                skuid: area[j].goodSkuId
              })
            } else {
              goodssuamlist.push(goodsobj)
              goodsindexlist.push(area[j].goodsId)
            }
            console.log(goodsobj)
          }
          console.log(goodssuamlist)
          obj.goodsList = goodssuamlist
          that.setData({
            shippingSongList: obj
          })
          // setTimeout(function () {
          //   wx.hideLoading()
          // }, 0)
        }
      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options)
    setTimeout(function() {
      wx.showLoading({
        title: '加载中',
      })
    }, 0)
    let flag
    if (options.flag == 'true') {
      flag = true
    } else {
      flag = false
    }
    this.setData({
      type: options.type,
      selectregionainfo: JSON.parse(options.selectregionainfo),
      dingdanTimes2: JSON.parse(options.dingdanTimes2),
      flag: flag
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.getshoppingList()
    this.getSongshoppingList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    let that = this
    let nowpage = that.data.nowpage
    nowpage = nowpage + 1
    let sumpage = that.data.sumpage
    if (nowpage <= sumpage) {
      setTimeout(function() {
        wx.showLoading({
          title: '加载中',
          mask: true,
        })
      }, 0)
      that.setData({
        nowpage: nowpage
      })
      that.getshoppingList()
    } else {
      wx.showToast({
        title: '已经到底了',
        icon: 'none'
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  // 加载更多
  addxiayiye() {
    let that = this
    setTimeout(function() {
      wx.showLoading({
        title: '加载中',
      })
    }, 0)
    that.setData({
      nowpage: that.data.nowpage + 1
    })
    that.getshoppingList()
  }
})





// 规格筛选
// let shippingList = res.data.beans
// for (let i in shippingList) {
//   let goodssuamlist = []
//   let area = shippingList[i].goodsList
//   for (let j in area) {
//     let goodsobj = {
//       goodsId: area[j].goodsId,
//       goodsname: area[j].goodsname,
//       guigelist: []
//     }
//     goodsobj.guigelist.push({
//       val: area[j].value1,
//       num: area[j].num,
//       skuid:area[j].goodSkuId
//     })
//     console.log(goodsobj)
//     if (goodssuamlist.length == 0) {
//       goodssuamlist.push(33333)
//     }else{
//       for (let k in goodssuamlist) {
//         // goodssuamlist.push(goodsobj)
//         if (goodssuamlist[k].goodsId == area[j].goodsId) {
//           goodssuamlist[k].guigelist.push({
//             val: area[j].value1,
//             num: area[j].num,
//              skuid: area[j].goodSkuId
//           })
//         } else {
//           goodssuamlist.push(goodsobj)
//         }
//       }
//     }
//   }
//   console.log(goodssuamlist)
//   shippingList[i].goodsList = []
//   shippingList[i].orderBeans = goodssuamlist
// }






// // 获取数据
// getshoppingList() {
//   let that = this
//   let timesobj = that.data.dingdanTimes2
//   let star, end
//   if (that.data.huoindex == 1) {
//     star = ''
//     end = ''
//   } else {
//     star = timesobj.yearstar + '-' + timesobj.monthstar + '-' + timesobj.daystar
//     end = timesobj.yearend + '-' + timesobj.monthend + '-' + timesobj.dayend
//   }
//   let recetownId
//   if (that.data.selectregionainfo.townId) {
//     recetownId = that.data.selectregionainfo.townId
//   } else {
//     recetownId = ''
//   }
//   wx.request({
//     url: app.globalData.myurl,
//     data: {
//       cmd: "shippingList",
//       userId: wx.getStorageSync('userId'),
//       shopId: wx.getStorageSync('shopId'),
//       type: that.data.type,
//       beginAdtime: star,
//       endAdtime: end,
//       nowPage: that.data.nowpage,
//       recetownId: recetownId
//     },
//     header: {
//       "content-type": "application/x-www-form-urlencoded"
//     },
//     success(res) {
//       console.log(res)
//       if (res.data.result == 1) {
//         setTimeout(function () {
//           wx.hideLoading()
//         }, 0)
//         setTimeout(function () {
//           wx.showToast({
//             title: res.data.resultNote,
//             icon: 'none',
//             duration: 1500
//           })
//         }, 0)
//       } else {
//         if (that.data.nowpage == 1) {
//           that.setData({
//             shippingList: res.data.beans
//           })
//         } else {
//           let list = that.data.shippingList
//           for (let i in res.data.beans) {
//             list.push(res.data.beans[i])
//           }
//           that.setData({
//             shippingList: list
//           })
//         }
//         setTimeout(function () {
//           wx.hideLoading()
//         }, 0)
//       }
//     }
//   });
// },